package com.ncu.cloundeoffice.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ncu.cloundeoffice.mapper.TEmployeeMapper;
import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.pojo.TEmployee;
import com.ncu.cloundeoffice.service.TEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Service
public class TEmployeeServiceImpl implements TEmployeeService {

    @Autowired
    private TEmployeeMapper tEmployeeMapper;

    /**
     * 查询所有的人员
     * @param currentPage
     * @param pageSize
     * @param tEmployee
     * @param beginDateScope
     * @return
     */
    @Override
    public PageInfo<TEmployee> getAllEmployee(int currentPage, int pageSize, TEmployee tEmployee, Date[] beginDateScope) {
        PageHelper.startPage(currentPage,pageSize);
        List<TEmployee> tEmployees = tEmployeeMapper.getAllEmployee(tEmployee,beginDateScope);
        PageInfo<TEmployee> pageInfo = new PageInfo<>(tEmployees);
        return pageInfo;
    }

    /**
     * 获取工号
     * @return
     */
    @Override
    public ResponseResult getMaxWorkId() {
        String workId = tEmployeeMapper.getMaxWorkId();
        String maxWorkId = String.format("%08d", Integer.parseInt(workId) + 1);
        return ResponseResult.success(null,maxWorkId);
    }

    /**
     * 添加员工
     * @param tEmployee
     * @return
     */
    @Override
    public ResponseResult addEmployee(TEmployee tEmployee) {
        Date beginContract = tEmployee.getBeginContract();
        Date endContract = tEmployee.getEndContract();
        long mile = endContract.getTime() - beginContract.getTime();
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        tEmployee.setContractTerm(Double.parseDouble(decimalFormat.format(mile/1000.00/60.00/60.00/24.00/365.00)));
        int count = tEmployeeMapper.addEmployee(tEmployee);
        if(count == 1){
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.error("添加失败");
    }

    /**
     * 更新员工
     * @param tEmployee
     * @return
     */
    @Override
    public ResponseResult updateEmployee(TEmployee tEmployee) {
        int count = tEmployeeMapper.updateEmployee(tEmployee);
        if(count == 1){
            return ResponseResult.success("更新成功");
        }
        return ResponseResult.error("更新失败");
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteEmployee(int id) {
        int count = tEmployeeMapper.deleteEmployee(id);
        if(count == 1){
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.error("删除失败");
    }

    /**
     * 查询员工 或者通过id查询
     * @param id
     * @return
     */
    @Override
    public List<TEmployee> getEmployeeById(Integer id) {
        return tEmployeeMapper.getEmployeeById(id);
    }

    /**
     * excel表添加员工
     * @param list
     * @return
     */
    @Override
    public int insertTEmployee(List<TEmployee> list) {
        return tEmployeeMapper.insertTEmployee(list);
    }


    /**
     * 获取所有员工工资账套
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult getEmployeeWithSalary(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<TEmployee> list = tEmployeeMapper.getEmployeeWithSalary();
        PageInfo<TEmployee> pageInfo = new PageInfo<>(list);

        return ResponseResult.success(null,pageInfo);
    }

    /**
     * 更新员工工资账套
     * @param eid
     * @param sid
     * @return
     */
    @Override
    public ResponseResult updateEmployeeSalary(int eid, int sid) {
        int count = tEmployeeMapper.updateEmployeeSalary(eid,sid);
        if(count == 1){
            return ResponseResult.success("更新成功!");
        }
        return ResponseResult.error("更新失败!");
    }


}
